***********
*Findings Age effects on PCRQ

*******
use "${clean_data}\gfs_cleaned_merged_database.dta", clear
cd "${output}\"

bysort  relation_structure: sum parent_relig [aw=weight]

global demo "male other_gender secondary tertiary child_poverty  age_group1 age_group2 age_group3 age_group4 age_group5 age_group6  foreign_born"
global demo_age "male other_gender secondary tertiary child_poverty  age foreign_born"

encode country_name, gen(country_num)


forval i = 1/4 {
gen B`i'=.
gen SE`i'=.
gen CB`i'=.
gen CSE`i'=.
}

forval i=1/22 {
reg PCRQ $demo_age  [aw=weight] if country_num==`i'
replace B1=_b[age] if country_num==`i'
replace SE1=_se[age] if country_num==`i'
}

forval i=1/22 {
reg PCRQ $demo  [aw=weight] if country_num==`i'
replace B2=_b[age_group1] if country_num==`i'
replace SE2=_se[age_group1] if country_num==`i'
}

forval i=1/22 {
reg PCRQ $demo  [aw=weight] if country_num==`i'
replace B3=_b[tertiary] if country_num==`i'
replace SE3=_se[tertiary] if country_num==`i'
}


forval i=1/22 {
reg PCRQ $demo  [aw=weight] if country_num==`i'
replace B4=_b[child_poverty] if country_num==`i'
replace SE4=_se[child_poverty] if country_num==`i'
}

gen yr_born=2023-age
tab yr_born
egen cohort=cut(yr_born), at(1900,1960,1980,1990,2000, 2050)
tab cohort, gen(cohort)


forval i=1/22 {
reg PCRQ male other_gender secondary tertiary child_poverty   foreign_born cohort1 cohort2 cohort3 cohort4 [aw=weight] if country_num==`i'
forval v=1/4 {
replace CB`v'=_b[cohort`v'] if country_num==`i'
replace CSE`v'=_se[cohort`v'] if country_num==`i'
}
}

gen very_dif_poverty=1 if child_poverty==4
replace very_dif_poverty=0 if child_poverty<4 & child_poverty>=1

collapse (count) obs=PCRQ (first) country_name B1 B2 B3 B4 SE1 SE2 SE3 SE4 CB1 CSE1 CB2 CSE2 CB3 CSE3 CB4 CSE4 ///
(mean) PCRQ thriving HOPE HAPPY ACCEPTED_BY_GOD FINANCIAL parent_relig_index self_relig_index parents_married parent_died  flove_no_dad mlove_no_mom was_abused child_poverty very_dif_poverty [aw=weight], by(country)


forval i = 1/4 {
gen T`i'=B`i'/SE`i'
gen CT`i'=CB`i'/CSE`i'
}

cor B1 T1 thriving HOPE HAPPY ACCEPTED_BY_GOD FINANCIAL parent_relig_index self_relig_index parents_married parent_died  flove_no_dad mlove_no_mom was_abused child_poverty 

gsort -T1
edit

aorder
order country obs country_name
export delimited "Effect of age on PCRQ by country.csv", replace

stop


************
**Birth cohort
*************
use "${clean_data}\gfs_cleaned_merged_database.dta", clear
cd "${output}\"
gen yr_born=2023-age

egen agecat=cut(age), at(18,25,30,35,40,45,50,55,60,65,70,75,110)

gen very_dif_poverty=1 if child_poverty==4
replace very_dif_poverty=0 if child_poverty<4 & child_poverty>=1

collapse (count) N=PCRQ (mean) PCRQ parent_relig_index self_relig_index parents_married parent_died  flove_no_dad mlove_no_mom was_abused child_poverty very_dif_poverty (semean) sePCRQ=PCRQ (min) minage=age (max) maxage=age [aw=weight], by( country agecat)

gen UP=PCRQ+(1.96*sePCRQ)
gen LO=PCRQ-(1.96*sePCRQ)

drop if N<100
edit
export delimited "PCRQ by age cohort by country.csv", replace